<template>
  <!-- 该标准库用于中交和中咨共用,参数type会有所区别 -->
  <div class="container">
    <el-row class="" style="margin-bottom: 20px">
      <el-col :span="16" align="left">
        <el-input
          v-model="searchFrom.param"
          placeholder="指标名称/编号"
          style="width: 150px; margin-right: 15px"
          @change="search"
          clearable
        />
        <el-button class="ordinary" @click="search()">搜索</el-button>
      </el-col>
      <el-col :span="8" align="right"   v-if="standardType==1">
        <el-button class="ordinary" @click="getExportBtn()" v-limit="'KHGL:KJKH:ZJKJKH:ZJBZZBK:EXPORT'">批量导出</el-button>
        <el-button type="danger" @click="handleDetect()" v-limit="'KHGL:KJKH:ZJKJKH:ZJBZZBK:DELETE'">删除</el-button>
        <el-button class="lock" @click="handleCopy()"  v-limit="'KHGL:KJKH:ZJKJKH:ZJBZZBK:COPY'">复制</el-button>
        <el-button type="primary" @click="handleAdd()"  v-limit="'KHGL:KJKH:ZJKJKH:ZJBZZBK:ADD'">新增指标</el-button>
      </el-col>

      <el-col :span="8" align="right"   v-if="standardType==2">
        <el-button class="ordinary" @click="getExportBtn()" v-limit="'KHGL:KJKH:ZZKJKH:ZZBZZBK:EXPORT'">批量导出</el-button>
        <el-button type="danger" @click="handleDetect()" v-limit="'KHGL:KJKH:ZZKJKH:ZZBZZBK:DELETE'">删除</el-button>
        <el-button class="lock" @click="handleCopy()"  v-limit="'KHGL:KJKH:ZZKJKH:ZZBZZBK:COPY'">复制</el-button>
        <el-button type="primary" @click="handleAdd()"  v-limit="'KHGL:KJKH:ZZKJKH:ZZBZZBK:ADD'">新增指标</el-button>
      </el-col>
    </el-row>
    <div>
      <el-table height="78vh" 
        :data="data.table_list"
        style="width: 100%"
        v-loading="data.table_loading"
        ref="qwltTable"
        row-key="id"
        @selection-change="selectionChange"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="selection"
          width="40"
          align="center"
          :reserve-selection="false" fixed="left"
        />
        <el-table-column prop="number" label="指标编号" width="140" align="left" fixed="left" />
        <el-table-column prop="name" label="指标名称" align="center"  width="180" fixed="left"  />
        <el-table-column
          prop="indexType_text"
          label="指标类型"
          align="center"
          width="100"
        />
        <el-table-column prop="scoreType_text" label="计分类型" align="center" width="100">
        </el-table-column>
        <el-table-column
          prop="scoreMethod_text"
          label="评分方式"
          align="center"
          width="100"
        >
        </el-table-column>
        <el-table-column prop="score" label="指标分数(H)" align="center" width="140" />
        <el-table-column
          prop="scoreCompose"
          label="系统计分组成"
          align="center"
          width="260"
        >
          <template #default="scope">
            {{ filterScoreCompose(scope.row.scoreCompose, scope.row.scoreSource) }}
          </template>
        </el-table-column>
        <el-table-column prop="indexIntro" label="指标说明" align="center" width="200" />
        <el-table-column prop="createUsername" label="创建人" align="center" width="100" />
        <el-table-column prop="createTime" label="创建时间" align="center"  width="150" />
        <el-table-column prop="modifyUsername" label="编辑人" align="center" width="100" />
        <el-table-column prop="modifyTime" label="编辑时间" align="center" width="150"  />
        <el-table-column prop="address" fixed="right" label="操作" align="center" width="220" >
          <template #default="scope" >
            <el-button class="ordinary" v-if="scope.row.id != null" @click="handleEdit(scope.row)" v-limit="'KHGL:KJKH:ZJKJKH:ZJBZZBK:EDIT'">编辑</el-button>
            <el-button
              type="primary"
              v-if="scope.row.scoreMethod === '1'"
              @click="giveScoreManage(scope.row)"
              v-limit="'KHGL:KJKH:ZJKJKH:ZJBZZBK:RATEMANAGER'"
              >评分管理</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <editAddStandardIndex
      v-model:visible="data.modalVisible"
      @refresh="gettargetlist"
      :opType="data.opType"
      :dictDatas="dictDatas"
      insertApi="addStandardIndexRequest"
      updateApi="editStandardIndexRequest"
      :assignParams="assignParams"
      :standardType="standardType"
      :editForm="data.handleRowData"
      :parentNumOption="data.table_list"
    ></editAddStandardIndex>
    <scoreManModal
      v-model:visible="data.scoreManVisible"
      :handleRow="data.handleRowData"
      :apiPackage="apiPackage"
    ></scoreManModal>
  </div>
</template>
<script lang="ts" setup>
import { nextTick, reactive, ref, watch } from "vue";
// import Table from '../../../components/indexTable.vue'
import { dataDictionary } from "@/api/dict";
import pubFc from "@/views/examineManage/utils";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRoute, useRouter } from "vue-router";
import * as api from "../../../api/examineManage";
import editAddStandardIndex from "./addAndEdit.vue";
import scoreManModal from "./scoreManModal.vue";

const searchFrom = reactive({
  //查询
  param: "",
});

// const refreshPage:any=inject('refreshPage', Function, true);
let standardType = ref("1"); //该参数决定显示的是中交还是中咨的标准库纸指标
const route = useRoute();
standardType.value = route.path=='/examineManage/techExamine/zjExamine/standardLibrary'?'1':'2';


const apiPackage = reactive({
  detail: "scoreManDetailRequest",
  score: "updateScoreRuleRequest",
  insert: "insertItemSetRequest",
  update: "updateItemSetRequest",
  delete: "deleteItemSetRequest",
});
const assignParams = reactive({
  type: standardType.value,
});
let data = reactive<any>({
  table_list: [],
  modalVisible: false,
  opType: "add", //add,edit,copy
  handleRowData: null, //正在处理的行数据
  scoreManVisible: false,
  table_loading: false,
  fixed: {
    type: Boolean,
  },
});
const dictDatas = reactive({
  dict_indexTypes: [],
  dict_indexNames: [],
  dict_scoreTypes: [],
  dict_scoreMethods: [],
  dict_scoreSources_zj: [],
  dict_scoreSources_zz: [],
  dict_scoreCompose_zj: [],
  dict_scoreCompose_zz: [],
}); //字典值
let selectedRows = ref<any>([]);
const search = () => {
  //搜索
  gettargetlist();
};
const filterScoreCompose = (scoreCompose, scoreSource) => {
  let str = "",
    dict_list = [];
  dict_list =
    standardType.value === "1"
      ? dictDatas.dict_scoreSources_zj
      : dictDatas.dict_scoreSources_zz;
  let cd: any = dict_list.find((it: any) => it.itemValue === scoreSource);
  dict_list = cd ? cd.children : [];
  if (scoreCompose && dict_list.length) {
    scoreCompose.map((item) => {
      let t = dict_list.find((item2) => (item2 as any).itemValue === item);
      if (t) {
        str += "+" + (t as any).itemText;
      }
    });
    str = str.substring(1);
  }
  return str;
};
const getExportBtn = () => {
  ElMessageBox.confirm("确定要导出数据吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
  //批量导出
  api
    .exportStandardIndexRequest({
      type: standardType.value,
      ...searchFrom,
    })
    .then((res) => {
      const { data, headers } = res;
      pubFc.downloadFunc(data, headers);
    });
  });
};
const selectionChange = (arr) => {
  //选中
  selectedRows.value = arr;
};
const handleDetect = () => {
  //批量删除

  if (selectedRows.value.length === 0) {
    ElMessage({
      message: "请选择数据后删除！",
      type: "warning",
    });
  } else if (selectedRows.value.length > 1) {
    ElMessage({
      message: "请选择一条数据！",
      type: "warning",
    });
  } else {
    ElMessageBox.confirm("确定要删除选中数据吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      // let sel_ids=selectedRows.value.map(item=>{return item.id});
      api
        .deleteStandardIndexRequest({
          id: selectedRows.value[0].id,
        })
        .then((res) => {
          if (res.data.code == 200) ElMessage.success("删除成功");
          gettargetlist();
        });
    });
  }
};
const getLockBtn = () => {
  //复制
};
const handleAdd = () => {
  //新增
  data.opType = "add";
  data.modalVisible = true;
};
const handleEdit = (item) => {
  //编辑
  data.opType = "edit";
  data.handleRowData = item;
  data.modalVisible = true;
};
const handleCopy = () => {
      if (selectedRows.value.length !== 1) {
        ElMessage({
          message: "请选择一条数据复制",
          type: "warning",
        });
      } else {
        ElMessageBox.confirm("确定要数据复制吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          data.opType = "copy";
          data.handleRowData = selectedRows.value[0];
          data.modalVisible = true;
        });
      }
};
const giveScoreManage = (item) => {
  ElMessageBox.confirm("确定要评分管理吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    //评分管理
    data.handleRowData = item;
    data.scoreManVisible = true;
  });
};
const getDictDatas = () => {
  return new Promise((resolve, reject) => {
    data.table_loading = true;
    let dictIds = [
      "KHGL_CLASS_INDEX_NAME_CODE",
      "KHGL_INDEX_TYPE_CODE",
      "KHGL_SCORE_METHOD_CODE",
      "KHGL_SCORE_TYPE_CODE",
      "KHGL_ZJ_EXAMINE_SCORE_METHOD_CODE",
      "KHGL_ZZ_EXAMINE_SCORE_METHOD_CODE",
    ];
    dataDictionary(dictIds).then((res) => {
      let response = res.data.data;
      dictDatas.dict_indexTypes = response.KHGL_INDEX_TYPE_CODE;
      dictDatas.dict_indexNames = response.KHGL_CLASS_INDEX_NAME_CODE;
      dictDatas.dict_scoreTypes = response.KHGL_SCORE_TYPE_CODE;
      dictDatas.dict_scoreMethods = response.KHGL_SCORE_METHOD_CODE;
      dictDatas.dict_scoreSources_zj = response.KHGL_ZJ_EXAMINE_SCORE_METHOD_CODE;
      dictDatas.dict_scoreSources_zz = response.KHGL_ZZ_EXAMINE_SCORE_METHOD_CODE;
      if (standardType.value === "1") {
        //中交
        response.KHGL_ZJ_EXAMINE_SCORE_METHOD_CODE.map((item) => {
          dictDatas.dict_scoreCompose_zj = dictDatas.dict_scoreCompose_zj.concat(
            item.children
          );
        });
      } else {
        response.KHGL_ZZ_EXAMINE_SCORE_METHOD_CODE.map((item) => {
          dictDatas.dict_scoreCompose_zz = dictDatas.dict_scoreCompose_zz.concat(
            item.children
          );
        });
      }

      resolve(1);
    });
  });
};
// let table_list=reactive<any>([]);
const qwltTable=ref<any>();
const gettargetlist = () => {
  //获取标准指标库列表
  data.table_loading = true;
  api
    .superIndexSdBanklistRequest({
      ...searchFrom,
      type: standardType.value, //1-中交标准指标、2-中咨标准指标
    })
    .then((res) => {
      data.table_loading = false;
      data.table_list = res.data.data;
      nextTick(()=>{
        if(qwltTable.value){  //使用了父子级后选中第一级再查询后这一级就无法清除了
          (qwltTable as any).value.clearSelection();
        }
      })

    });
};

let router = useRouter()
watch(() =>router.currentRoute.value.path,(newValue,oldValue)=> {
  standardType.value = route.path=='/examineManage/techExamine/zjExamine/standardLibrary'?'1':'2';
  console.log('standardType.value',standardType.value);

  gettargetlist()
},{ immediate: true })

getDictDatas().then(() => {
  gettargetlist();
});
</script>
<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
  overflow: hidden;

}

// :deep(.el-dialog__body) {
//   padding: 0 !important
// }
:deep(.el-dialog__body) {
  padding: 0;
}

.dialogCloseBtn {
  position: absolute;
  top: 20px;
  right: 20px;
}

// .el-col {
//   line-height: 60px;
// }

.pageName {
  font-weight: 700;
}

.el-form-item {
  .el-input {
    width: 100px;
  }

  .el-select {
    width: 100px;
  }
}

:deep(.demo-tabs > .el-tabs__content) {
  max-height: 500px;
  overflow-y: scroll;
  scrollbar-width: none;
  /* Firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
}

:deep(.demo-tabs > .el-tabs__content::-webkit-scrollbar) {
  display: none;
  /* Chrome Safari */
}
</style>
